package com.sl.au.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.neo4j.cypher.internal.compiler.v2_2.perty.recipe.Pretty.page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import com.sl.au.service.OperateLogService;

import com.sl.au.entity.Account;
import com.sl.au.entity.User;
import com.sl.au.entity.Evaluate;
import com.sl.au.entity.BusinesssellerAuthentication;
import com.sl.au.entity.BusinesssellerBaseInfo;
import com.sl.au.entity.Option;
import com.sl.au.entity.PartBaseInfo;
import com.sl.au.repository.UserRepository;
import com.sl.au.repository.EvaluateRepository;
import com.sl.au.repository.BusinesssellerBaseInfoRepository;
import com.sl.au.repository.BusinesssellerAuthenticationRepository;
import com.sl.au.service.AccountService;
import com.sl.au.service.UserService;
import com.sl.au.service.OptionService;

@Service("userService")
@Transactional
public class UserServiceImpl extends BaseServiceImpl implements UserService {

	@Autowired
	UserRepository userRepository;
	@Autowired
	AccountService accountService;
	@Autowired
	OperateLogService operateLogService;
	@Autowired
	OptionService optionService;
	@Autowired
	BusinesssellerAuthenticationRepository businesssellerAuthenticationRepository;
	@Autowired
	BusinesssellerBaseInfoRepository businesssellerBaseInfoRepository;
	@Autowired
	EvaluateRepository evaluateRepository;

	@Override
	public List<User> getPage(int start, int length, String key, String orderName, String dir) {
		Page<User> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		pages = userRepository.findAll(pageable);
		return pages.getContent();
	}

	@Override
	public List<User> getUsers(int start, int length, String key, String orderName, String dir) {
		Page<User> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		pages = userRepository.getUserName(key, pageable);
		return pages.getContent();
	}
	
	@Override
	public List<User> getUsersByDept(int start, int length, String key, String deptId,String orderName, String dir) {
		Page<User> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		if(StringUtils.hasText(key)){
			pages = userRepository.getUserNameByDeptWithKey(key, deptId, pageable);
		}else{
			pages =userRepository.getUserNameByDept(deptId, pageable);
		}
		return pages.getContent();
	}
	@Override
	public int getCountByDept(String deptId,String key){
		if (!StringUtils.hasText(key))
			return userRepository.getCountByDept(deptId);
		return userRepository.getCountByDeptWithKey(key, deptId);
	}

	public UserRepository getRepository() {
		return this.userRepository;
	}

	@Override
	public int getCount(String key) {
		if (!StringUtils.hasText(key))
			return userRepository.findAll().size();
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return userRepository.getUserName(key, pageable).getContent().size();
	}

	@Override
	public void delete(String userId) {
		User user = this.getRepository().getOne(userId);
		if (null == user)
			return;
		accountService.getRepository().deleteByUser(user);
		this.getRepository().delete(userId);

	}

	@Override
	public int patch(User user, User currentUser) {
		int code = 0;
		if (code == 0) {
			User old = this.userRepository.findOne(user.getId());
			if (null != old) {
				BeanUtils.copyProperties(user, old, "createTime");
				this.userRepository.save(old);
				operateLogService.log(currentUser, "修改用户信息", "修改了用户信息:" + old.getName());
			}
		}
		return code;
	}

	@Override
	public int saveUser(User user) {
		int code = 0;
		boolean isOk = false;
		if (this.userRepository.findOne(user.getId()) == null) {
			isOk = true;
		}
		this.userRepository.save(user);
		List<Option> option = optionService.getRepository().findByGroupAndTitle("信息状态", "初始化");
		BusinesssellerBaseInfo baseinfo = new BusinesssellerBaseInfo();
		baseinfo.setManagementPerson(user);

		/*
		 * if (isOk) { baseinfo.setBussinessLevel(user.getUserLevel()); }
		 */
		baseinfo.setInfoState(option.get(0));
		baseinfo.setCodeAdress("/ui/bussinessmain?device=mobile&&identify=" + baseinfo.getId());
		this.businesssellerBaseInfoRepository.save(baseinfo);
		BusinesssellerAuthentication au = new BusinesssellerAuthentication();
		au.setManagementPerson(user);
		au.setBusinesssellerBaseInfo(baseinfo);
		au.setInfoState(option.get(0));
		this.businesssellerAuthenticationRepository.save(au);

		Evaluate evaluate = new Evaluate();
		evaluate.setEvaluateScale(100);// 初始分数100
		evaluate.setBusinesssellerBaseInfo(baseinfo);
		this.evaluateRepository.save(evaluate);
		return code;
	}

	@Override
	public List<User> userByRole(String roleName) {
		List<User> users = this.getRepository().findAll();
		List<User> alluser = new ArrayList<User>();
		for (User u : users) {
			Account account = accountService.getRepository().findByUser(u);
			if (account != null) {
				String role = account.getRole().getName();
				if (role.indexOf(roleName) != -1) {
					alluser.add(u);
				}
			}

		}
		return alluser;
	}

	@Override
	public boolean findByName(String name) {
		User user = userRepository.findByName(name);
		return user == null;
	}

	@Override
	public List<User> getUserByNoGrading(int start, int length, String key, String orderName, Direction dir) {
		Page<User> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			pages = userRepository.getByNoGradingAndKey(key, pageable);
		} else {
			pages = userRepository.getAllByNoGrading(pageable);
		}
		return pages.getContent();
	}

	@Override
	public int getCountByNoGrading(String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		Page<User> pages = null;
		if (StringUtils.hasText(key)) {
			pages = this.userRepository.getByNoGradingAndKey(key, pageable);
		} else {
			pages = userRepository.getAllByNoGrading(pageable);
		}
		return pages.getContent().size();
	}

	@Override
	public List<User> getUserByGrading(int start, int length, String gradingId, String key, String orderName,
			Direction dir) {
		Page<User> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			pages = userRepository.getByGradingAndKey(gradingId, key, pageable);
		} else {
			pages = userRepository.getAllByGrading(gradingId, pageable);
		}
		return pages.getContent();
	}

	@Override
	public int getCountByGrading(String gradingId, String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		Page<User> pages = null;
		if (StringUtils.hasText(key)) {
			pages = userRepository.getByGradingAndKey(gradingId, key, pageable);
		} else {
			pages = userRepository.getAllByGrading(gradingId, pageable);
		}
		return pages.getContent().size();
	}

	/*
	 * @Override public int getUnbindGrid(String key) { Pageable pageable = new
	 * PageRequest(0, Integer.MAX_VALUE); if (!StringUtils.hasText(key)) {
	 * return
	 * userRepository.getUnbindGridUsersPage(pageable).getContent().size(); }
	 * return userRepository.getUnbindGridPageByKey(key,
	 * pageable).getContent().size(); }
	 */

	@Override
	public List<User> getBindUsersPage(int start, int length, String key, String orderName, Direction dir) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");

		return userRepository.getBindUsersPage(key, pageable).getContent();
	}

	@Override
	public int getBindUserCount(String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return userRepository.getBindUsersPage(key, pageable).getContent().size();
	}

	@Override
	public List<User> getBindHierarchyUsersPageBykey(int start, int length, String key, String orderName,
			Direction dir) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return userRepository.getBindHierarchyUsersPageBykey(key, pageable).getContent();
	}

	@Override
	public int getBindHierarchyUsersCount(String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return userRepository.getBindHierarchyUsersPageBykey(key, pageable).getContent().size();
	}

	@Override
	public List<User> getUnbindHierarchyUsersPageBykey(int start, int length, String key, String orderName,
			Direction dir) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return userRepository.getUnbindHierarchyUsersPageBykey(key, pageable).getContent();
	}

	@Override
	public Integer getUnbindHierarchyUserCount(String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		if (!StringUtils.hasText(key)) {
			return userRepository.getUnbindHierarchyUsersPage(pageable).getContent().size();
		}
		return userRepository.getUnbindHierarchyUsersPageBykey(key, pageable).getContent().size();
	}

	@Override
	public List<User> getUnbindHierarchyUsersPage(int start, int length, String key, String orderName, Direction dir) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return userRepository.getUnbindHierarchyUsersPage(pageable).getContent();
	}

	@Override
	public List<User> getBindHierarchyUsersPageBykeyAndCon(int start, int length, String key, String con) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return userRepository.getBindHierarchyUsersPageBykeyAndCon(key, con, pageable).getContent();
	}

	@Override
	public int getBindHierarchyUsersCountByCon(String key, String con) {

		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return userRepository.getBindHierarchyUsersPageBykeyAndCon(key, con, pageable).getContent().size();
	}

	@Override
	public List<User> getBindUsersPageByKeyAndCon(int start, int length, String gridId, String con) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return userRepository.getBindUsersPageByKeyAndCon(gridId, con, pageable).getContent();
	}

	@Override
	public int getBindUserCountByCon(String gridId, String con) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return userRepository.getBindUsersPageByKeyAndCon(gridId, con, pageable).getContent().size();
	}

	@Override
	public List<User> getAllBindHierarchyUsersByKeyAndCon(int start, int length, String con) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return userRepository.getAllBindHierarchyUsersByKeyAndCon(con, pageable).getContent();
	}

	@Override
	public int getAllBindHierarchyUsersCountByKeyAndCon(String con) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return userRepository.getAllBindHierarchyUsersByKeyAndCon(con, pageable).getContent().size();
	}

	@Override
	public List<User> getBindHierarchyUsersPageNone(int start, int length, String key, String string, Object object) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return userRepository.getBindHierarchyUsersPageNone(pageable).getContent();
	}

	@Override
	public int getBindHierarchyUsersCountNone(String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return userRepository.getBindHierarchyUsersPageNone(pageable).getContent().size();
	}

	@Override
	public List<User> findBindByConOnly(String con, int start, int length) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return userRepository.findBindByConOnly(con, pageable).getContent();
	}

	@Override
	public int findBindCountByConOnly(String con) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return userRepository.findBindByConOnly(con, pageable).getContent().size();
	}

	@Override
	public int findUsersUnbindHierarchyCountByKeyAndCon(String key, String con) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return userRepository.findUsersUnbindHierarchyByKeyAndCon(key, con, pageable).getContent().size();
	}

	@Override
	public List<User> findUsersUnbindHierarchyByKeyAndCon(String key, String con, int start, int length) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return userRepository.findUsersUnbindHierarchyByKeyAndCon(key, con, pageable).getContent();
	}

	@Override
	public List<User> findUserUnbindHierarchyOnlyKey(String key, int start, int length) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return userRepository.findUserUnbindHierarchyOnlyKey(key, pageable).getContent();
	}

	@Override
	public int findUserUnbindHierarchyCountOnlyKey(String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return userRepository.findUserUnbindHierarchyOnlyKey(key, pageable).getContent().size();
	}

	@Override
	public List<User> findUsersUnbindHierarchyOnlyCon(String con, int start, int length) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return userRepository.findUsersUnbindHierarchyOnlyCon(con, pageable).getContent();
	}

	@Override
	public int findUsersUnbindHierarchyCountOnlyCon(String con, int start, int length) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return userRepository.findUsersUnbindHierarchyOnlyCon(con, pageable).getContent().size();
	}

	@Override
	public List<User> findUsersUnbindHierarchyNone(int start, int length) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return userRepository.findUsersUnbindHierarchyNone(pageable).getContent();
	}

	@Override
	public int findUsersUnbindHierarchyCountNone() {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return userRepository.findUsersUnbindHierarchyNone(pageable).getContent().size();
	}

	@Override
	public List<User> findUsersUnbindEventTypeOnlyCon(int start, int length, String con) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return userRepository.findUsersUnbindEventTypeOnlyCon(pageable, con).getContent();
	}

	@Override
	public int findUsersCountUnbindEventTypeOnlyCon(String con) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return userRepository.findUsersUnbindEventTypeOnlyCon(pageable, con).getContent().size();
	}

	@Override
	public List<User> findUsersUnbindEventTypeNone(int start, int length) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return userRepository.findUsersUnbindEventTypeNone(pageable).getContent();
	}

	@Override
	public int findUsersCountUnbindEventTypeNone() {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return userRepository.findUsersUnbindEventTypeNone(pageable).getContent().size();
	}

	@Override
	public List<User> findUsersUnbindEventTypeByKeyAndCon(int start, int length, String key, String con) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return userRepository.findUsersUnbindEventTypeByKeyAndCon(pageable, key, con).getContent();
	}

	@Override
	public int findUsersCountUnbindEventTypeByKeyAndCon(String key, String con) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return userRepository.findUsersUnbindEventTypeByKeyAndCon(pageable, key, con).getContent().size();
	}

	@Override
	public List<User> findUsersUnbindEventTypeOnlyKey(int start, int length, String key) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return userRepository.findUsersUnbindEventTypeOnlyKey(pageable, key).getContent();
	}

	@Override
	public int findUsersCountUnbindEventTypeOnlyKey(String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return userRepository.findUsersUnbindEventTypeOnlyKey(pageable, key).getContent().size();
	}

	@Override
	public List<User> findUsersBindEventTypeOnlyCon(int start, int length, String con) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return userRepository.findUsersBindEventTypeOnlyCon(pageable, con).getContent();
	}

	@Override
	public int findUsersCountBindEventTypeOnlyCon(String con) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return userRepository.findUsersBindEventTypeOnlyCon(pageable, con).getContent().size();
	}

	@Override
	public List<User> findUsersBindEventTypeNone(int start, int length) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return userRepository.findUsersBindEventTypeNone(pageable).getContent();
	}

	@Override
	public int findUsersCountBindEventTypeNone(int start, int length) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return userRepository.findUsersBindEventTypeNone(pageable).getContent().size();
	}

	@Override
	public List<User> findUsersBindEventTypeByKeyAndCon(int start, int length, String key, String con) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return userRepository.findUsersBindEventTypeByKeyAndCon(pageable, key, con).getContent();
	}

	@Override
	public int findUsersCountBindEventTypeByKeyAndCon(String key, String con) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return userRepository.findUsersBindEventTypeByKeyAndCon(pageable, key, con).getContent().size();
	}

	@Override
	public List<User> findUsersBindEventTypeOnlyKey(int start, int length, String key) {
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		return userRepository.findUsersBindEventTypeOnlyKey(pageable, key).getContent();
	}

	@Override
	public int findUsersCountBindEventTypeOnlyKey(String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
		return userRepository.findUsersBindEventTypeOnlyKey(pageable, key).getContent().size();
	}

	@Override
	public List<User> getUsersByEventTypeAndGrid(int start, int length, String eventTypeId, String gridId, String orderName, String dir) {
		Page<User> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		pages = userRepository.findUsersByEventTypeAndGrid(eventTypeId, gridId, pageable);
		return pages.getContent();
	}

	@Override
	public int getUsersByEventTypeAndGridCount(String eventTypeId, String gridId) {
		int count = 0;
		count = userRepository.findUsersByEventTypeAndGridCount(eventTypeId,gridId);
		return count;
	}

}
